<?php

namespace App\HttpController\Api;

use App\Model\Banners\BannersBean;
use App\Model\Banners\BannersModel;
use EasySwoole\MysqliPool\Mysql;
use EasySwoole\Validate\Validate;
use EasySwoole\Http\Message\Status;
/**
 * 用户表
 * Class Users
 * Create With Automatic Generator
 */
class Banners extends Base
{
    public function getAll()
    {
        $db = Mysql::defer('mysql');
        $param = $this->request()->getRequestParam();
        $page = ((int)$param['page'])??1;
        $limit = ((int)$param['limit'])??20;
        $model = new BannersModel($db);
        $data = $model->getAll($page, $param['keyword']??null, $limit);
        $this->writeJson(Status::CODE_OK, $data, 'success');
    }

    public function add()
    {
        $db    = Mysql::defer('mysql');
        $param = $this->request()->getRequestParam();
        $model = new BannersModel($db);
        $bean  = new BannersBean();
        $bean->setTitle($param['title']);
        $bean->setImage($param['image']);
        $bean->setUrl($param['url']);
        $bean->setPublishtime($param['publishtime']);
        $bean->setEndtime($param['endtime']);
        $bean->setSwitch($param['switch']);
        $rs = $model->add($bean);
        if ($rs) {
            $this->writeJson(Status::CODE_OK, $bean->toArray(), "success");
        } else {
            $this->writeJson(Status::CODE_BAD_REQUEST, [], $db->getLastError());
        }
    }

    protected function getValidateRule(?string $action): ?Validate
    {
        switch ($action){
            case 'add':
                $valitor = new Validate();
                $valitor->addColumn('title')->required();
                $valitor->addColumn('image')->required();
                $valitor->addColumn('endtime')->required();
                return $valitor;
                break;
        }
        // TODO: Implement getValidateRule() method.
        return null;
    }


}

